package com.adminserver;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.adminserver.controller.AnnouncementController;
import com.adminserver.controller.WorkerController;
import com.adminserver.customEntity.AnnouncementEntity;
import com.adminserver.customEntity.SaltEntity;
import com.adminserver.customEntity.SystemAccountEntity;
import com.adminserver.customEntity.UnionUserRoleEntity;
import com.adminserver.entity.*;
import com.adminserver.mapper.*;
import com.adminserver.service.*;
import com.adminserver.service.impl.UserRoleServiceImpl;
import com.adminserver.util.DateGenerator.DateGenerator;
import com.adminserver.util.FormatUserRoleData;
import com.adminserver.util.RedisUtil;
import com.adminserver.util.rsaUtil.GenerateKey;
import com.adminserver.util.rsaUtil.RsaUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;

import static com.adminserver.controller.AnnouncementController.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AdminServerApplicationTests {

    Jedis redis = RedisUtil.getRedis();

    @Autowired
    RsaUtil rsaUtil;

    @Autowired
    AdminService adminService;

    @Autowired
    RepairOrdersMapper repairOrdersMapper;

    @Autowired
    WorkerService workerService;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    AdminJobRecordMapper adminJobRecordMapper;

    @Autowired
    UserService userService;

    @Autowired
    ApplyMaterialService applyMaterialService;

    @Autowired
    ApplyMaterialMapper applyMaterialMapper;

    @Autowired
    UserRoleMapper userRoleMapper;


    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    SystemAccountEntity systemAccountEntity;

    @Autowired
    SaltEntity saltEntity;


    @Test
    public void AdminServerApplicationTests_101() {
    }

    @Test
    public void AdminServerApplicationTests_99() {
        HashMap<String, List<AnnouncementEntity>> result = new HashMap();
        if (redis.exists(ANNOUNCEMENT_RANGE.get(ALL))) {
            result.put(ALL, formatRedisAnnouncement(ALL));
        }
        if (redis.exists(ANNOUNCEMENT_RANGE.get(USER))) {
            result.put(USER, formatRedisAnnouncement(USER));
        }
        if (redis.exists(ANNOUNCEMENT_RANGE.get(WORKER))) {
            result.put(WORKER, formatRedisAnnouncement(WORKER));
        }
        Console.log(result);
    }

    @Test
    public void AdminServerApplicationTests_111() {
        AnnouncementEntity announcementEntity = new AnnouncementEntity();
        for (int i = 0; i < 5; i++) {
            redis.hset(AnnouncementController.ANNOUNCEMENT_RANGE.get(ALL), RandomUtil.randomString(6), JSON.toJSONString(announcementEntity));
            redis.hset(AnnouncementController.ANNOUNCEMENT_RANGE.get(USER), RandomUtil.randomString(6), JSON.toJSONString(announcementEntity));
            redis.hset(AnnouncementController.ANNOUNCEMENT_RANGE.get(AnnouncementController.WORKER), RandomUtil.randomString(6), JSON.toJSONString(announcementEntity));
        }
    }

    @Test
    public void AdminServerApplicationTests_861() {
        String str = "Q/7BWz4tCcFWTZozqpRvovON5WRFhlxjiQmi2As3pKfDXI4GQNUV3jGdpOiwHIlNR8+R3w2Fmbv+o/2+1N72CVleCR1+YmFZ3ITLZw47Ur9+XkqTT9pY4gWl6GQoKtE2cnHDcJNlxAAt0ly93BB2vR6kS12+yjtMj9pneR/O2uQ=";
        String decrypt = rsaUtil.RsaPriDecrypt(str, GenerateKey.cacheKey.get(GenerateKey.PUBLIC), GenerateKey.cacheKey.get(GenerateKey.PRIVATE));
        System.out.println(decrypt);
    }

    @Test
    public void AdminServerApplicationTests_86() {
        System.out.println(DateUtil.date().month());
    }


    @Test
    public void AdminServerApplicationTests_82() {
        String[] yearInMonthBeginEndTime = DateGenerator.getYearInMonthBeginEndTime(DateUtil.date().year(), DateUtil.date().month());
        System.out.println(yearInMonthBeginEndTime[0]);
        System.out.println(yearInMonthBeginEndTime[1]);
        yearInMonthBeginEndTime = DateGenerator.getYearInMonthBeginEndTime(DateUtil.date().year(), DateUtil.date().offset(DateField.MONTH, -1).month());
        System.out.println("---------------------------------");
        System.out.println(yearInMonthBeginEndTime[0]);
        System.out.println(yearInMonthBeginEndTime[1]);
    }

    @Test
    public void AdminServerApplicationTests_65() {
        // 去年同比增长率
        DateTime lastYear = DateUtil.date().offset(DateField.YEAR, -1);
        DateTime lastTwoYear = DateUtil.date().offset(DateField.YEAR, -2);
        System.out.println(lastYear);
        System.out.println(lastTwoYear);
        System.out.println("-------------------------");
        String[] lastYear_yearBeginEndTime = DateGenerator.getYearBeginEndTime(lastYear.year());
        String[] lastTwoYear_yearBeginEndTime = DateGenerator.getYearBeginEndTime(lastTwoYear.year());
        Console.log(lastYear_yearBeginEndTime);
        Console.log(lastTwoYear_yearBeginEndTime);
        List<ApplyMaterialEntity> lastYearList = applyMaterialMapper.queryMaterialPriceAndTakeAmountAndChargeOnByApplyTimeBetween(lastYear_yearBeginEndTime[0], lastYear_yearBeginEndTime[1]);
        List<ApplyMaterialEntity> lastTwoYearList = applyMaterialMapper.queryMaterialPriceAndTakeAmountAndChargeOnByApplyTimeBetween(lastTwoYear_yearBeginEndTime[0], lastTwoYear_yearBeginEndTime[1]);
        double lastYearTotalSpending = 0, lastTwoYearTotalSpending = 0;
        for (ApplyMaterialEntity item : lastYearList) {
            lastYearTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        for (ApplyMaterialEntity item : lastTwoYearList) {
            lastTwoYearTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        lastYearTotalSpending = NumberUtil.round(lastYearTotalSpending, 2).doubleValue();
        lastTwoYearTotalSpending = NumberUtil.round(lastTwoYearTotalSpending, 2).doubleValue();
        double growthRate = NumberUtil.round(NumberUtil.div((lastYearTotalSpending - lastTwoYearTotalSpending), lastYearTotalSpending), 2).doubleValue() * 100;
        System.out.println(lastYear.year() + "年较" + lastTwoYear.year() + "报修支出同比支出增长率：" + growthRate);
    }

    @Test
    public void AdminServerApplicationTests_96() {
        int year = DateUtil.date().year();
        int lastMonth = DateUtil.date().offset(DateField.MONTH, -1).month();
        int lastTwoMonth = DateUtil.date().offset(DateField.MONTH, -2).month();
        String[] lastMonthBeginEndTime = DateGenerator.getYearInMonthBeginEndTime(year, lastMonth);
        String[] lastTwoMonthBeginEndTime = DateGenerator.getYearInMonthBeginEndTime(year, lastTwoMonth);
        Console.log(lastMonthBeginEndTime);
        Console.log(lastTwoMonthBeginEndTime);
        List<ApplyMaterialEntity> lastMonthBeginEndList = applyMaterialMapper.queryMaterialPriceAndTakeAmountByApplyTimeBetween(lastMonthBeginEndTime[0], lastMonthBeginEndTime[1]);
        List<ApplyMaterialEntity> lastTwoMonthBeginEndList = applyMaterialMapper.queryMaterialPriceAndTakeAmountByApplyTimeBetween(lastTwoMonthBeginEndTime[0], lastTwoMonthBeginEndTime[1]);
        double lastMonthTotalSpending = 0, lastTwoMonthTotalSpending = 0;
        for (ApplyMaterialEntity item : lastMonthBeginEndList) {
            lastMonthTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        for (ApplyMaterialEntity item : lastTwoMonthBeginEndList) {
            lastTwoMonthTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        lastMonthTotalSpending = NumberUtil.round(lastMonthTotalSpending, 2).doubleValue();
        lastTwoMonthTotalSpending = NumberUtil.round(lastTwoMonthTotalSpending, 2).doubleValue();
        String lastMonthGrowthRate = NumberUtil.decimalFormat(".##%", NumberUtil.round(NumberUtil.div((lastMonthTotalSpending - lastTwoMonthTotalSpending), lastTwoMonthTotalSpending), 2).doubleValue());

        System.out.println(year + "年" + (lastMonth + 1) + "月报修环比支出增长率：" + lastMonthGrowthRate);

    }


    @Test
    public void AdminServerApplicationTests_123() {
        // 2018-2021年均增长量
        String[] years = new String[2];
        years[0] = DateUtil.format(DateUtil.date().offset(DateField.YEAR, -1), DatePattern.NORM_DATETIME_PATTERN);
        years[1] = DateUtil.format(DateUtil.date().offset(DateField.YEAR, -4), DatePattern.NORM_DATETIME_PATTERN);
    }

    @Test
    public void AdminServerApplicationTests_132() {
        int[] years = new int[2];
        years[0] = DateUtil.date().offset(DateField.YEAR, -1).year();
        years[1] = DateUtil.date().offset(DateField.YEAR, -4).year();
        String[] currentYear = DateGenerator.getYearBeginEndTime(years[0]);
        String[] baseYear = DateGenerator.getYearBeginEndTime(years[1]);
        List<ApplyMaterialEntity> currentYearList = applyMaterialMapper.queryMaterialPriceAndTakeAmountByApplyTimeBetween(currentYear[0], currentYear[1]);
        List<ApplyMaterialEntity> baseYearList = applyMaterialMapper.queryMaterialPriceAndTakeAmountByApplyTimeBetween(baseYear[0], baseYear[1]);
        double currentYearTotalSpending = 0, baseYearTotalSpending = 0;
        for (ApplyMaterialEntity item : currentYearList) {
            currentYearTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        for (ApplyMaterialEntity item : baseYearList) {
            baseYearTotalSpending += NumberUtil.mul(item.getMaterialPrice(), item.getTakeAmount()).doubleValue();
        }
        double yearAverageGrowthIncrease = NumberUtil.div(NumberUtil.sub(currentYearTotalSpending, baseYearTotalSpending), baseYearTotalSpending);
        System.out.println(years[1] + "-" + years[0] + "年报修支出年均增长量：" + yearAverageGrowthIncrease);

        double v = NumberUtil.div(currentYearTotalSpending, baseYearTotalSpending);
        double n = 1d / years[0] - years[1];
        String yearAverageGrowthRate = NumberUtil.decimalFormat(".##%", Math.pow(v, n) - 1);
        System.out.println(years[1] + "-" + years[0] + "年报修支出年均增长率：" + yearAverageGrowthRate);
    }

    @Test
    public void AdminServerApplicationTests_155() {
        String[] yearBeginEndTime = DateGenerator.getYearBeginEndTime(2022);
        List<RepairOrdersEntity> list = repairOrdersMapper.queryAllByRepairDateBetween(yearBeginEndTime[0], yearBeginEndTime[1]);
        list.forEach(System.out::println);
    }

    @Test
    public void AdminServerApplicationTests_162() {
        int a = 3, b = 9;
        System.out.println(NumberUtil.div(9, 0));
    }


    @Test
    public void AdminServerApplicationTests_168() {
        RolePermissionEntity entity = new RolePermissionEntity();
        entity.setRoleId("1524642387031244802");
        //entity.setPermissionId("1524696862743875585");
        entity.setPermissionId("1524696903835471873");
        System.out.println(rolePermissionService.save(entity));
    }


    @Test
    public void AdminServerApplicationTests_196() {
        String[] roleIds = new String[]{"1524642387031244802", "1524642948199759874"};
        LambdaQueryWrapper<UserRoleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UserRoleEntity::getRoleId, roleIds);

        System.out.println(userRoleService.list(lambdaQueryWrapper));
    }


    public static void main(String[] args) {
        HashMap<String, Object> map = null;

        String[] years = new String[2];
        if (ObjectUtil.isEmpty(map)) {
            years = DateGenerator.getYearBeginEndTime(DateUtil.date().year());
        }
        Console.log(years);

        //String[] createYear = new String[2];
        //if (years[0].equals(years[1])) createYear = DateGenerator.getYearBeginEndTime(years[0]);


    }


    @Test
    public void AdminServerApplicationTests_79() {
        System.out.println(DateGenerator.RandomDateTime());
    }

    @Test
    public void AdminServerApplicationTests_59() {
        List<ApplyMaterialEntity> applyMaterialEntities = applyMaterialService.yearSpendingAndIncomeFinancial(2022);
        applyMaterialEntities.forEach(System.out::println);
    }

    @Test
    public void AdminServerApplicationTests_50() {
        ArrayList<UserEntity> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserUsername(RandomUtil.randomNumbers(10));
            userEntity.setUserPassword(RandomUtil.randomString(6));
            userEntity.setUserNickname("用户" + i);
            userEntity.setCreateTime(DateUtil.date());
            list.add(userEntity);
        }
        userService.saveBatch(list);
    }

    @Test
    public void AdminServerApplicationTests_72() {
        Page<UserEntity> page = new Page<>(1, 100);
        userService.page(page);
        List<UserEntity> records = page.getRecords();
        records.forEach(System.out::println);
        System.out.println("是否有下一页：" + page.hasNext());
        System.out.println("是否有上一页：" + page.hasPrevious());
        System.out.println("总记录数：" + page.getTotal());
    }

    @Test
    public void AdminServerApplicationTests_45() {
        AdminJobRecordEntity adminJobRecordEntity = adminJobRecordMapper.queryOnGuardByAccount("15281056531");
        System.out.println(adminJobRecordEntity);
    }

    @Test
    public void AdminServerApplicationTests_42() {
        List<AdminEntity> adminEntities = adminMapper.queryAllAdminAndState();
        System.out.println(adminEntities);
    }

    @Test
    public void AdminServerApplicationTests_49() {
        System.out.println(redis.keys("*"));
    }


    @Test
    public void AdminServerApplicationTests_146() {
        for (int i = 0; i < 10; i++) {
            redis.setex("add_worker:" + RandomUtil.randomString(10),
                    60 * 60,
                    JSON.toJSONString(new WorkerEntity()));
        }
    }

    @Test
    public void AdminServerApplicationTests_52() {
        for (int i = 0; i < 10; i++) {
            WorkerEntity workerEntity = new WorkerEntity();
            workerEntity.setWorkerPhone(RandomUtil.randomNumbers(11));
            workerEntity.setWorkerName(RandomUtil.randomString(3));
            redis.setex("add_worker:" + RandomUtil.randomNumbers(6),
                    60 * 60, JSON.toJSONString(workerEntity));
        }
    }

    @Test
    public void AdminServerApplicationTests_66() {
        redis.select(0);
        Set<String> keys = redis.keys("add_worker:*");
        List<String> mget = redis.mget(keys.toArray(new String[]{}));
        mget.forEach(System.out::println);
    }
}

